package com.pengtu.web.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Department;
import com.pengtu.entity.app.InsideOrganization;
import com.pengtu.service.app.AccountManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 内部单位管理
 * 
 * @author libitum
 * 
 */
@Namespace("/account")
@Results({ @Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "inside_organization.action") })
public class InsideOrganizationAction extends
		BaseActionSupport<InsideOrganization> {

	private static final long serialVersionUID = 1L;

	private AccountManager accountManager;

	// -- 页面属性 --//
	private String id;
	private InsideOrganization entity;
	private List<InsideOrganization> allInsideOrganizationList;
	private Page<InsideOrganization> page = new Page<InsideOrganization>(10);// 每页5条记录
	private InsideOrganization organization;
	public AccountManager getAccountManager() {
		return accountManager;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public InsideOrganization getEntity() {
		return entity;
	}

	public void setEntity(InsideOrganization entity) {
		this.entity = entity;
	}

	public List<InsideOrganization> getAllInsideOrganizationList() {
		return allInsideOrganizationList;
	}

	public Page<InsideOrganization> getPage() {
		return page;
	}

	public void setPage(Page<InsideOrganization> page) {
		this.page = page;
	}

	@Override
	public InsideOrganization getModel() {
		// TODO Auto-generated method stub
		return entity;
	}

	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		queryfilter.addFilter("type", "0");
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchInsideOrganization(page, queryfilter);
		return SUCCESS;
	}

	@Action("/account/api/inside_organization")
	public String apilist() throws Exception {
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		queryfilter.addFilter("type", "0");
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchInsideOrganization(page, queryfilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		allInsideOrganizationList = accountManager.getInsideOrganization();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		entity.setOrganization(organization);
		accountManager.saveInsideOrganization(entity);
		addActionMessage("保存单位成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		accountManager.deleteInsideOrganization(id);
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			entity = accountManager.getInsideOrganization(id);
		} else {
			entity = new InsideOrganization();
		}
	}
	
	/**
	 * 验证单位名是否重复
	 * @throws Exception 
	 */
	public String checkOrgName() throws Exception {
		// TODO Auto-generated method stub
		String orgName = Struts2Utils.getParameter("orgName");
		String oldOrgName = Struts2Utils.getParameter("oldOrgName");
		if (accountManager.isOrgNameUnique(orgName, oldOrgName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		return null;
	}

	@Action("/account/json/inside_organization")
	public String searchForTree() throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<InsideOrganization> organizations = accountManager
				.getInsideOrganization();

		for (InsideOrganization organization : organizations) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("expanded", Boolean.TRUE);
			map.put("text", organization.getOrgName());
			map.put("id", organization.getId());
			List<Department> depList = organization.getDepartment();
			if (depList.isEmpty()) {
				map.put("leaf", Boolean.TRUE);
			} else {
				List<Map<String, Object>> departmentList = new ArrayList<Map<String, Object>>();
				for (Department department : depList) {
					Map<String, Object> depMap = new HashMap<String, Object>();
					depMap.put("leaf", Boolean.TRUE);
					depMap.put("text", department.getDepName());
					depMap.put("id", department.getId()+"_"+department.getOrganization().getId());
					departmentList.add(depMap);
				}
				map.put("children", departmentList);
			}
			list.add(map);
		}
		Struts2Utils.renderJson(list, "no-cache");
		return null;
	}

	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkOrgNum() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newOrgName = request.getParameter("orgName");
		String oldOrgName = request.getParameter("oldOrgName");

		if (accountManager.isOrgNameUnique(newOrgName, oldOrgName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		// 因为直接输出内容而不经过jsp,因此返回null.
		return null;
	}

	public InsideOrganization getOrganization() {
		return organization;
	}

	public void setOrganization(InsideOrganization organization) {
		this.organization = organization;
	}
	
	

}
